<template>
  <view v-if="shenheStatus == 0" class="page">
    <pageHeader :status="status"></pageHeader>

    <pageList :items="list"></pageList>

    <pagePop ref="pop" :item="item"></pagePop>

    <pageCancalPop ref="cancalPop" :item="item"></pageCancalPop>
    <toolsMore :status="moreStatus"></toolsMore>
  </view>
  <Shenhe v-else-if="shenheStatus == 1"></Shenhe>
</template>

<script>
import Shenhe from "@/components/shenhe/shenhe";
import toolsMore from "@/components/tools/tools-more.vue";
import pageHeader from "./components/page-header.vue";
import pageList from "../components/page-list.vue";
import pagePop from "./components/page-pop.vue";
import pageCancalPop from "../components/page-cancal-pop.vue";
import { orderList } from "@/api/marketing/callOut";

export default {
  components: {
    Shenhe,
    toolsMore,
    pageHeader,
    pageList,
    pagePop,
    pageCancalPop,
  },

  data() {
    return {
      list: [],
      moreStatus: 1,
      page: 1,
      isEnd: false,
      status: "-1",
      item: null,
    };
  },

  async onLoad() {
    this.clearData();
  },

  onShow() {
    this.Emit.$on("handle", this.handle);
  },

  onHide() {
    this.Emit.$off("handle");
  },

  onUnload() {
    this.Emit.$off("handle");
  },

  methods: {
    handle(type, params) {
      switch (type) {
        case "status":
          this.status = params;
          this.clearData();
          break;

        case "pop":
          this.item = params;
          this.$refs.pop.show = true;
          break;

        case "cancal":
          this.item = params;
          this.$refs.cancalPop.show = true;
          break;

        case "rest":
          this.clearData();
          break;

        case "goItem":
          this.$utils.toUrl(
            `/marketing/serviceHome/order/item/index?id=${params.id}`
          );
          break;

        case "evaluate":
          this.$utils.toUrl(
            `/marketing/serviceHome/order/evaluate/index?id=${
              params.id
            }&goods_ids=${params.goods_list.map((el) => el.goods_id).join(",")}`
          );
          break;

        default:
      }
    },

    clearData() {
      this.page = 1;
      this.isEnd = false;
      this.list = [];
      this.getList();
    },

    async getList() {
      try {
        let { list: oldList, page, status } = this;
        this.moreStatus = 2;
        const res = await orderList({
          page,
          status,
        });
        uni.stopPullDownRefresh();
        this.moreStatus = 1;
        if (res.code == 0) {
          let { list: data, totalCount } = res.data,
            count = parseInt(totalCount);
          if (data.length) {
            oldList.push(...data);
            this.page = page + 1;
          }
          if (oldList.length >= count) {
            this.isEnd = true;
            this.moreStatus = 3;
          }
        }
      } catch (error) {
        this.$utils.toast(error);
      }
    },
  },

  onReachBottom() {
    if (this.isEnd) {
      return;
    }

    this.getList();
  },

  onPullDownRefresh() {
    this.clearData();
  },
};
</script>

<style lang="scss">
.page {
  min-height: 100vh;
  background: #f2f2f2;
  overflow: hidden;
}
</style>
